Bug 5561

Bug Description:

This is a race.
The private filed physicalConnection_ in class org.apache.derby.client.am.LogicalConnection is not proper protected from multiple threads accessing.
A NullPointerException could be raised due to this bug.
More details about this bug are at DERBY-5561 JIRA page.

Interleaving Description:

The following shows the buggy interleavings between different threads.

t1  t2   
2       logicalConnection.nullPhysicalConnection(); 
        
org.apache.derby.client.am.LogicalConnection
        
        public String nativeSQL(String sql) throws SQLException {
            try {
    1           checkForNullPhysicalConnection();
    3            return physicalConnection_.nativeSQL(sql);
            } catch (SQLException sqle) {
                notifyException(sqle);
                throw sqle;
            }
        }

a) thread 2 checks if physicalConnection_ is null at 1. And it is not null.
b) context switched, thread 1 set physicalConnection_ to null at 2.
c) context switched, thread 2 calls a method, but physicalConnection_ is null now. so a NullPointerException is thrown.

How To Reproduce:

This bug is reproduced under derby 10.5.1.1 and JDK 1.6.0_33.
Execute the following scripts to run the test to reproduce the bug (assume the location of the derby test project is derby_test_home).

Linux:
${derby_test_home}/scripts/5561.sh
Windows:
%derby_test_home%\scripts\5561.bat